﻿package com.eitb.botones{
	import flash.geom.*;
	import flash.text.*;
	import flash.events.*;
	import flash.display.*;
	import com.eitb.events.*;
	import com.eitb.formaters.*;
	import com.eitb.literales.*;

	public class VerticalScrollBar extends Boton implements IVistaScrollBar {
		private static const MIN:Number=0;
		private var maxPosPointer:Number;
		private var mover:Boolean;

		private var pointer:DisplayObject;
		private var background:DisplayObject;

		private var textField:TextField;

		public function VerticalScrollBar():void {
			this.mover=false;
			this.pointer = new Sprite();
			this.background = new Sprite();

			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
		}

		public function initText(estilo):void {
			var styleSheet:StyleSheet = new StyleSheet();
			this.textField=new TextField();
			this.addChild(this.textField);

			styleSheet.setStyle(".estilo", estilo);

			this.textField.x=0;
			this.textField.y=this.background.height/2-this.textField.height/2+10;
			this.textField.width=this.background.width;
			this.textField.height=this.background.width;

			this.textField.styleSheet=styleSheet;
			//this.textField.filters = new Array(new BlurFilter(2, 2, BitmapFilterQuality.HIGH));

			this.textField.selectable=false;
			this.textField.autoSize=TextFieldAutoSize.CENTER;
		}
		public function initPointer(pointer:DisplayObject):void {
			this.pointer=pointer;
			this.addChild(this.pointer);
		}

		public function initBackground(background:DisplayObject):void {
			this.background=background;
			this.addChild(this.background);
			this.maxPosPointer=this.background.height;
		}
		// INTERFAZ FUTURA
		public function setVisible(mostrar:Boolean):void {
			this.visible=mostrar;
		}
		public function setText(text:String):void {
			this.textField.htmlText="<span class='estilo'>".concat(text).concat("</span>");
		}

		public function setPointer(number:Number):void {
			this.pointer.height = this.maxPosPointer * (1 - number);
		}

		public function mouseMove(event:MouseEvent):void {
			if (this.mover) {
				var porcentaje:Number = 1 - (this.background.mouseY / this.maxPosPointer);
				if (porcentaje < 0) {
					porcentaje = 0;
				} else if (porcentaje > 1) {
					porcentaje = 1;
				}
				
				this.dispatchEvent(new ScrollEvent(ScrollEvent.POINT_SELECTED, {porcentaje:porcentaje}));
			}
		}

		public function mouseUp(event:MouseEvent):void {
			this.mover=false;
			this.mouseMove(event);
		}
		// INTERFAZ FUTURA
		private function mouseDown(event:MouseEvent):void {
			var point:Point=localToGlobal(new Point(event.currentTarget.mouseX,event.currentTarget.mouseY));

			if (this.background.hitTestPoint(point.x,point.y)||this.pointer.hitTestPoint(point.x,point.y)) {
				this.mover=true;
				this.mouseMove(event);
			}
		}
	}
}